package Dynamic_programming;

import org.junit.Test;

import java.util.*;

/**
 * @Description: 链表获取随机节点：
 * @Param: $
 * @return: $
 * @Author 万家欣
 * @Date: 2022/8/4
 * Algorithm
 * @Version 1.0
 */
public class Solution {
    List<Integer> list = new ArrayList<>();
    Random r = new Random();
    public Solution(ListNode head) {
        while (head != null){
            list.add(head.val);
            head = head.next;
        }
    }

    public Solution() {
    }

    public int getRandom() {
        int index = r.nextInt(list.size());
        return list.get(index);
    }
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map  = new HashMap<>();
        for (int i = 0; i < nums.length - 1; i++) {
               int a = target - nums[i];
               if(map.containsKey(a)){
                   return new int[]{i,map.get(a)};
               }else {
                   map.put(a,i);
               }
        }
        return new int[]{};
    }

}
